<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends MY_Controller {


	public function __construct(){
		parent::__construct();
		$this->CI = & get_instance (); // 调用CI核心对象
		$this->CI->load->library ( 'session' );
	}
	public function index()
	{
		$callback = $this->input->get("callback");
		$username = $this->input->get("username");
		$password = $this->input->get("password");
		
		$re = $this->checkUser($username);
		if($re){
			$msg = "该用户不存在";
			$code = 2001;
		}else{
			$res = $this->checkPwd($username,$password);
			if($res){
				$msg = "登录成功";
				$code = 2000;
				$this->session->set_userdata($username);
			}else{
				$msg = "密码错误";
				$code = 2002;
			}
		}
		
		$result = array(
			"msg" => $msg,
			"code" => $code
		);
		
		$result_json = json_encode($result);
		echo $callback."($result_json)";
	}
	
	public function checkPwd($username,$password){
		$sql = "select password from future_u_c_user where username = '{$username}'";
		$data = $this->db->query($sql)->row_array();
		if($password == $this->_decrypt($data['password'])){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 密码解密
	 */
	private function _decrypt($str) {
		$this->load->library ( 'encrypt' );
		$re = $this->encrypt->decode ( $str );
		return $re;
	}
}